package com.orkasgb.framework.hadoop.mapperreduce.wordcount;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class WordCountDriver {

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        final Configuration configuration = new Configuration();
        // 设置wordcount计算时是否忽略大小写
        configuration.setBoolean("wordcount.case.sensitive", true);

        Job job = Job.getInstance(configuration);

        // 设置job名称
        job.setJobName("WordCount");

        job.setJarByClass(WordCountDriver.class);

        // 指定Mapper和Reduce
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReduce.class);

        // 设置Mapper输出K/V类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        // F:\hadoop\input\wordcount\wordcount.txt F:\hadoop\input\wordcount\wordcount01.txt
        // 默认是TextInputFormat，按文件切片，按行读数据，切片数量为2，开启两个MapTask。
        // 对于大量小文件，CombineTextInputFormat，按照多个文件进行切片，切片数据为1，开启一个MapTask.
        // job.setInputFormatClass(CombineTextInputFormat.class);
        // job.setCombinerClass();
        job.setNumReduceTasks(2);

        // 设置最终输出K/V类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        // 指定输入输出文件
        FileInputFormat.setInputPaths(job, new Path("F:\\hadoop\\input\\wordcount\\wordcount.txt"), new Path("F:\\hadoop\\input\\wordcount\\wordcount01.txt"));
        FileOutputFormat.setOutputPath(job, new Path("F:\\hadoop\\output\\wordcount\\wordcount12"));

        final boolean wait = job.waitForCompletion(true);
        System.exit(wait ? 0 : 1);
    }
}
